const normalCallout = {
  id: 1,
  latitude: 23.098994,
  longitude: 113.322520,
  iconPath: '../resources/location.png',
  callout: {
    content: '文本内容',
    color: '#ff0000',
    fontSize: 14,
    borderWidth: 2,
    borderRadius: 10,
    borderColor: '#000000',
    bgColor: '#fff',
    padding: 5,
    display: 'ALWAYS',
    textAlign: 'center'
  },
  // label: {
  //   content: 'label 文本',
  //   fontSize: 24,
  //   textAlign: 'center',
  //   borderWidth: 1,
  //   borderRadius: 5,
  //   bgColor: '#fff',
  //   padding: 5
  // }
  
}

const customCallout1 = {
  id: 2,
  latitude: 23.097994,
  longitude: 113.323520,
  iconPath: '../resources/location.png',
  customCallout: {
    anchorY: 0,
    anchorX: 0,
    display: 'ALWAYS'
  },
}

const customCallout2 = {
  id: 3,
  latitude: 23.096994,
  longitude: 113.324520,
  iconPath: '../resources/location.png',
  customCallout: {
    anchorY: 10,
    anchorX: 0,
    display: 'ALWAYS',
  },
}

const customCallout3 = {
  id: 4,
  latitude: 23.095994,
  longitude: 113.325520,
  iconPath: '../resources/location.png',
  customCallout: {
    anchorY: 0,
    anchorX: 20,
    display: 'ALWAYS',
  },
}

// const allMarkers = [normalCallout, customCallout1, customCallout2, customCallout3]

const {
  http
} = require("../../../utils/apiutils")



Page({
  data: {
    cl:{

    },
    markers: [],
    customCalloutMarkerIds: [],
    num: 1,
    mapshow:false,
    sh: 0,
    chooseModel:false,
    //  type   1需求  2供给  3岗位  4车辆
    type:3,
  iconArr:['http://linghuoimage.xbbzp.com/FmwXdiIsGcLnn-_8u-9RjYg5V109','http://linghuoimage.xbbzp.com/FkBAG1Tr6r3_aUkjl3ekOuVkenrP','http://linghuoimage.xbbzp.com/Fvvfi5oFet7zWOnEOLXFuwlaN9BL','http://linghuoimage.xbbzp.com/FnJfnsOR45Us20cbLszxZ80A6V0W'],
  showinfo:false,
  chooseMarker:{},
  markerId:0,
  chooseInfo:{}
  },
  onLoad: function () {
    var that = this;
    wx.getSystemInfo({
      success: (result) => {
        that.setData({
          sh: result.windowHeight
        })
      },
    })
  },
  onReady: function (e) {
    this.mapCtx = wx.createMapContext('myMap')
  },

  addMarker() {

  },

  removeMarker() {
    this.setData({
      markers: [],
      customCalloutMarkerIds: []
    })
  },

  changeMarkerId() {
    const customCalloutMarkerIds = this.data.customCalloutMarkerIds.slice()
    const top = customCalloutMarkerIds.shift()
    customCalloutMarkerIds.push(top)

    this.setData({
      customCalloutMarkerIds
    })
  },
  markertap(e) {
    let id = e.detail.markerId
    let item = this.data.markers.filter(item=>{
      return item.id == id
    })
    this.setData({
      markerId:id,
      chooseMarker:item[0]
    })
    wx.nextTick(_=>{
      this.nearbyInfo()
    })
  },
  nearbyInfo(){
    console.log(this.data.chooseMarker)
    http.nearbyInfo(this.data.chooseMarker,res=>{
      if(res.code==0){
        this.setData({
          chooseInfo:res.data,
          showinfo:true
        })
      }else{
        wx.showToast({
          title: res.message,
          icon:'none'
        })
      }
    },err=>{})
  },
  callouttap(e) {
    if (!getApp().globalData.isLogin) {
      wx.showModal({
        title: "提示",
        content: "您还没有登录，登陆后即可查看详情",
        confirmText: "去登陆",
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/login/login?ptype=nearby',
            })
          }
        }
      })
      return
    }
    return 
      wx.navigateTo({
      url: '/pages/home/pages/work/work?id=' + e.detail.markerId,
    })
    console.log('@@@ callouttap', e)
  },
  labeltap(e) {
    console.log('@@@ labeltap', e)
  },
  translateMarker: function () {
    const length = this.data.markers.length
    if (length === 0) return
    const index = Math.floor(Math.random() * length)
    const markers = this.data.markers
    const marker = markers[index]
    marker.latitude = marker.latitude*1 
    marker.longitude = marker.longitude*1 
    const that = this
    this.mapCtx.translateMarker({
      markerId: marker.id,
      duration: 1000,
      destination: {
        latitude: marker.latitude*1,
        longitude: marker.longitude*1
      },
      animationEnd() {
        that.setData({
          markers
        })
      },
      complete(res) {
        console.log('translateMarker', res)
      }
    })
  },
  changeContent() {
    const num = Math.floor(Math.random() * 10)
    this.setData({
      num
    })
  },
  //获取附近的岗位
  jobnearby: function (lat, lng) {
    var data = {
      lng: lng,
      lat: lat,
      distance: 50,
      count: 20,
    }
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    http.jobnearby(data,
      function (res) {
        wx.hideLoading()
        console.log("------附近的岗位---", res);
        if (res.code == 0) {
          var arr = res.data;
          var markers = []
          arr.forEach(element => {
            var customCallout = {
              id: element.id,
              latitude: element.lat*1,
              longitude: element.lng*1,
              width:30,
              height:20,
              name:element.name,
              pr:element.salary/100 +"元/" + element.payType,
              iconPath: '../resources/location.png',
              customCallout: {
                anchorY: 0,
                anchorX: 20,
                display: 'ALWAYS',
              },
            }
            markers.push(customCallout);
          });

          that.setData({
        
            markers: markers

          })
        } else {
          wx.showToast({
            title: res.message,
            icon: "none",
          })
        }
      },
      function (err) {
        wx.hideLoading()
      })
  },
  onShareAppMessage: function () {
    var shareObj = {
      title: "分享岗位", // 默认是小程序的名称(可以写slogan等)
      path: getApp().globalData.userInfo.inviteCode?'/pages/index/index?code='+getApp().globalData.userInfo.inviteCode:'/pages/index/index', // 
      imageUrl: '',
      success: function (res) {
        // 转发成功之后的回调
        if (res.errMsg == 'shareAppMessage:ok') {
        }
      }
    }
    console.log("------------", shareObj);
    // 返回shareObj
    return shareObj;
  },
  onShow: function () {
    var that = this;
    wx.getLocation({
      success: function (res) {
        console.log(res);
        var data =({
           latitude:res.latitude*1,
           longitude: res.longitude*1
        })
        that.setData({
           cl:data,
           mapshow:true,
        })
        // that.jobnearby(data.latitude, data.longitude)
        that.listNearby(data.latitude, data.longitude)
      },
    })
  },
  listNearby(lat,lng,distance){
    let params={
      lat,
      lng,
      distance:distance?distance:10,
      type:3
    }
    let _this = this;
    http.listNearby(params,res=>{
      if(res.code==0){
        this.setData({
          dataList:res.data
        })
        wx.nextTick(_=>{
          this.chooseData()
        })
      }else{
        wx.showToast({
          title: res.message,
          icon:"none"
        })
      }
    },err=>{})
  },
  changeType(e){
    let index=e.currentTarget.dataset.index;
    this.setData({
      type:index,
      showinfo:false
    })
    wx.nextTick(_=>{
      this.chooseData()
    })
  },
  showModal(){
    this.setData({
      chooseModel:!this.data.chooseModel
    })
  },
  chooseData(){
    let _this  =this;
    let markers = this.data.dataList.filter(item=>{
      item.latitude =item.lat*1;
      item.longitude =item.lng*1;
      item.iconPath = _this.data.iconArr[_this.data.type-1];
      item.width = 30;
      item.height =36;
      return item.type==_this.data.type
    })
    this.setData({
      markers,
      chooseModel:false
    })
    wx.nextTick(_=>{
      this.translateMarker()
    })
  },
  toType(){
    let url = '';
    let _this  =this;
    switch(this.data.chooseMarker.type){
      case 1:
        url="/pages/home/pages/searchGDtl/index?id="+_this.data.chooseInfo.id
        break;
      case 2:
        url="/pages/home/pages/searchGDtl/index?id="+_this.data.chooseInfo.id
        break;
      case 3:
        url="/pages/home/pages/work/work?id="+_this.data.chooseInfo.id
        break;
      case 4:
        url="/pages/home/pages/searchcar/searchcar"
        break;
    }
    wx.navigateTo({
      url,
    })
  },
  mapMove(e){
      let _this = this;
      if(e.type=='end'&&(e.causedBy == 'drag'||e.causedBy == 'scale')){
        // this.mapCtx.getCenterLocation()
        let dist = parseInt(e.detail.scale)-10;
        dist = dist>0?1:dist*-1+3;
        let data={
          latitude:e.detail.centerLocation.latitude*1,
          longitude: e.detail.centerLocation.longitude*1
        }
        _this.setData({
          cl:data
        })
        console.log(dist*10)
        wx.nextTick(_=>{
          _this.listNearby(data.latitude*1,data.longitude*1,dist*10)
        })
      }
  }
})